<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2004-2015 All rights reserved.
// +----------------------------------------------------------------------
// | Author: 张盛翔
// +----------------------------------------------------------------------
// | QQ：1213857382
// +----------------------------------------------------------------------
// | Module Discript: 后端接口示例
// +----------------------------------------------------------------------
// 定义命名空间
namespace Rest\Controller;
// 使用自定义类
use Rest\Controller\BaseController;
// 继承自定义类
Class IndexController extends BaseController {
	// 子函数
	public function test()
	{
		$data['info'] = "this  is  a test . Hello  world";
		switch ($this->_method){
			case 'get': // get请求处理代码
				$this -> respond("success",$data,$this->_type);
				break;
			case 'put': // put请求处理代码
				$this -> respond("success",$data,$this->_type);
				break;
			case 'post': // post请求处理代码
				$this -> respond("success",$data,$this->_type);
				break;
		}
	}

	public function login()
	{
		switch ($this->_method){
			case 'get': // get请求处理代码
				$data['errorcode'] = "No program parameter";
				$this -> respond("error",$data,$this->_type);
				break;
			case 'put': // put请求处理代码
				$data['errorcode'] = "No program parameter";
				$this -> respond("error",$data,$this->_type);
				break;
			case 'post': // post请求处理代码
				$mess = $_POST;
				if (!$_POST['account']||!$_POST['password']) {
					if (!$_POST['account']){
						$data['errorcode'] = "用户名不能为空";
					}
					if(!$_POST['password']){
						$data['errorcode'] = "密码不能为空";
					}
					$this -> respond("error",$data,$this->_type);
					break;
				}
				$finduser = M('user') -> where(array('account' => $_POST['account'],'password'=> $_POST['password'] )) -> find();
				if ($finduser) {
					// 存在用户
					session_start();
					$_SESSION = $finduser;
					$this -> respond("success",$_SESSION,$this->_type);
				}else{
					// 不存在用户
					$data['errorcode'] = "用户名和密码错误";
					$this -> respond("error",$data,$this->_type);
				}
				break;
		}
	}

	public function logout()
	{
		switch ($this->_method){
			case 'get': // get请求处理代码
				session_destroy();
				$data['info'] = "成功退出";
				$this -> respond("success",$data,$this->_type);
				break;
			case 'put': // put请求处理代码
				$this -> respond("success",$data,$this->_type);
				break;
			case 'post': // post请求处理代码
				$this -> respond("success",$data,$this->_type);
				break;
		}
	}


	public function reg()
	{
		switch ($this->_method){
			case 'get': // get请求处理代码
				$data['errorcode'] = "Program parameter error";
				$this -> respond("error",$data,$this->_type);
				break;
			case 'put': // put请求处理代码
				$data['errorcode'] = "Program parameter error";
				$this -> respond("error",$data,$this->_type);
				break;
			case 'post': // post请求处理代码
				$mess = $_POST;
				if (!$_POST['account']||!$_POST['password']||!$_POST['nickname']) {
					if (!$_POST['account']){
						$data['errorcode'] = "用户名不能为空";
					}
					if(!$_POST['password']){
						$data['errorcode'] = "密码不能为空";
					}
					if(!$_POST['nickname']){
						$data['errorcode'] = "昵称不能为空";
					}
					$this -> respond("error",$data,$this->_type);
					break;
				}
				$finduser = M('user') -> field('account') -> where(array('account' => $_POST['account'] )) -> find();
				if ($finduser) {
					// 存在用户
					$data['errorcode'] = "该用户名已存在,请尝试使用其他用户名注册";
					$this -> respond("error",$data,$this->_type);
				}else{
					// 不存在用户
					$createuser = M('user') -> add($_POST);
					if ($createuser) {
						$data['userid'] = $createuser;
						$this -> respond("success",$data,$this->_type);
					}else{
						$data['errorcode'] = "Program parameter error";
						$this -> respond("error",$data,$this->_type);
					}
				}

				break;
		}
	}

 }